#pragma once
#include "epoll/epoller.h"
#include "http/http_connect.h"
#include "thread_pool/thread_pool.h"
#include <map>
#include <memory>

namespace zh
{
    class TimerManager;

    class WebServer
    {
    public:
        WebServer();
        ~WebServer();

        void initialize(int port);
        void clear();

        void start();

    private:
        bool initSocket(int port);
        // void initEventMode();

        bool connectClient();
        bool disconnectClient(int clientSocket);

        HttpConnectionSharedPtr getClient(int clientSocket) const;
        bool                    resetTimerTask(int clientSocket);

        bool readData(int clientSocket);
        bool writeData(int clientSocket);

        void onRead(int clientSocket);
        void onWrite(int clientSocket);

        void onProcess(HttpConnectionSharedPtr client);

        int m_port;
        int m_serverSocket;

        Epoller m_epoller;
        bool    m_isRunning {false};
        int     m_timeout {-1};

        using FD_t      = int;
        using TimerID_t = int;
        std::map<FD_t, HttpConnectionSharedPtr> m_userMap;
        std::map<FD_t, TimerID_t>               m_timerMap;

        int m_serverEvent {0};
        int m_clientEvent {0};

        std::shared_ptr<TimerManager> m_timerManager;
        std::shared_ptr<ThreadPool>   m_taskRunner;

        static int s_RequestWaitQueueSize;
    };
} // namespace zh